package com.dto;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 仅作为微信小程序配置文件信息生成
 */
@Data
@Component
@Configuration
@ConfigurationProperties("wx")
public class Login {

    private String api;

    private String method;

    private String js_code; //需要的变化 code 参数

    private String openid;

    private Map<String, String> params;

    /**
     * 直接返回全 URL 接口+参数地址，用于 GET 直接的链接请求
     * @return String
     */
    public String toGetRequestURL(String code) {
        addJsCodeParam(code);
        StringBuilder sb = new StringBuilder(api);
        if (params.size() > 0) sb.append('?');
        for (String key : params.keySet()) {
            sb.append(key).append('=').append(params.get(key)).append('&');
        }
        if (sb.charAt(sb.length() - 1) == '&') sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    /**
     * 添加请求参数微信用户临时 code 码到 map params 参数对象里
     * @param code
     */
    public void addJsCodeParam(String code) {
        params.put(js_code, code);
    }

    public Map<String, String> getParams(String code) {
        addJsCodeParam(code);
        return params;
    }

}
